package com.next.aappublicapp;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.facebook.Session;
import com.next.aappublicapp.server.service.DataServices;
import com.next.aappublicapp.tasks.FacebookAsyncTask;
import com.next.aappublicapp.util.AlertDialogUtil;
import com.next.aappublicapp.util.FacebookUtil;
import com.next.aappublicapp.util.SystemUiHider;
import com.next.aappublicapp.util.TrackerUtl;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 * 
 * @see SystemUiHider
 */
public class PostFacebookCommentActivity extends Activity {
	
	public static final String ARGS_FACEBOO_POST_ID = "ARGS_FACEBOO_POST_ID"; 
	public static final String ARGS_BUNDLE = "ARGS_BUNDLE";
	
	private Button postButton;
	private EditText postText;
	private LinearLayout postContentLayout;
	private LinearLayout postingStatusLayout;
	
	private String postId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_post_facebook_comment);

		Bundle bundle = getIntent().getBundleExtra(ARGS_BUNDLE);
		postId = bundle.getString(ARGS_FACEBOO_POST_ID);
		postButton = (Button)findViewById(R.id.post_comment_button);
		postText = (EditText)findViewById(R.id.post_comment_text);
		postContentLayout = (LinearLayout)findViewById(R.id.postContentLayout);
		postingStatusLayout = (LinearLayout)findViewById(R.id.posting_status);
		//final View controlsView = findViewById(R.id.fullscreen_content_controls);
		//final View contentView = findViewById(R.id.fullscreen_content);
		postButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Log.i("Ravi", "Clicked on Like button");
				String mesage = postText.getText().toString();
				commentOnPost(mesage);

			}
		});
		
	}
	
	private void commentOnPost(final String mesage){
		Log.i("Ravi", "Liking post");
		if(!FacebookUtil.isFacebookLoggedIn()){
			AlertDialogUtil.showDIalog(this, "Error", "You must login via facebook to use this feature");
			return;
		}
		FacebookUtil.checkPermission(this, "publish_stream");
		new FacebookAsyncTask<String, String, String>("FacebookPost", "Comment") {
			@Override
			protected void beforeExecute() {
				postContentLayout.setVisibility(View.GONE);
				postingStatusLayout.setVisibility(View.VISIBLE);
			}
			@Override
			protected String doInBackground(String... params) {
				try {
					Log.i("Ravi", "postId="+postId);
					Log.i("Ravi", "mesage="+mesage);
					return DataServices.getInstance().commentOnFacebookPost(postId,Session.getActiveSession().getAccessToken(),mesage);
				} catch (Exception e) {
					e.printStackTrace();
				}
				return null;
			}
			@Override
			protected void afterExecute(String result) {
				postContentLayout.setVisibility(View.VISIBLE);
				postingStatusLayout.setVisibility(View.GONE);
				if(result == null){
					TrackerUtl.sendFacebookEvent("CommentFailed");
				}else{
					PostFacebookCommentActivity.this.finish();
					TrackerUtl.sendFacebookEvent("Comment");
				}
				
			}
		}.execute();
		
	}
}
